<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Webpack基础配置(二) | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="前言 问：为什么使用babel? webpack只理解ES5语法的js文件，并不理解ES6+的语法及js新的API。 babel可以将ES6+的语法及js新的API转换称ES5语法。    下载babel及相关 开发时依赖下载： yarn add babel-loader @babel&#x2F;core @babel&#x2F;preset-env @babel&#x2F;plugin-transform-runtime -">
<meta property="og:type" content="article">
<meta property="og:title" content="Webpack基础配置(二)">
<meta property="og:url" content="http://example.com/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%BA%8C/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="前言 问：为什么使用babel? webpack只理解ES5语法的js文件，并不理解ES6+的语法及js新的API。 babel可以将ES6+的语法及js新的API转换称ES5语法。    下载babel及相关 开发时依赖下载： yarn add babel-loader @babel&#x2F;core @babel&#x2F;preset-env @babel&#x2F;plugin-transform-runtime -">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7a0e872f73bc4a8f892825d477e8cff1~tplv-k3u1fbpfcp-watermark.image">
<meta property="og:image" content="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6e08bd4415074996b84325d229d36a29~tplv-k3u1fbpfcp-watermark.image">
<meta property="article:published_time" content="2020-12-20T11:45:16.000Z">
<meta property="article:modified_time" content="2020-12-20T11:45:48.241Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7a0e872f73bc4a8f892825d477e8cff1~tplv-k3u1fbpfcp-watermark.image">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-Webpack基础配置-二" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%BA%8C/" class="article-date">
  <time class="dt-published" datetime="2020-12-20T11:45:16.000Z" itemprop="datePublished">2020-12-20</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      Webpack基础配置(二)
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><ul>
<li>问：为什么使用babel?<ul>
<li>webpack只理解ES5语法的js文件，并不理解ES6+的语法及js新的API。</li>
<li>babel可以将ES6+的语法及js新的API转换称ES5语法。</li>
</ul>
</li>
</ul>
<h2 id="下载babel及相关"><a href="#下载babel及相关" class="headerlink" title="下载babel及相关"></a>下载babel及相关</h2><ol>
<li>开发时依赖下载：<br> <code>yarn add babel-loader @babel/core @babel/preset-env @babel/plugin-transform-runtime --dev</code></li>
</ol>
<ol start="2">
<li>运行时依赖：<br> <code>yarn add @babel/runtime-corejs3 core-js@3</code></li>
</ol>
<h2 id="babel-config-js文件-babel配置文件"><a href="#babel-config-js文件-babel配置文件" class="headerlink" title="babel.config.js文件(babel配置文件)"></a>babel.config.js文件(babel配置文件)</h2><p>在项目根目录下创建<code>babel.config.js</code>或<code>.babelrc</code>文件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    <span class="string">&quot;presets&quot;</span>: [</span><br><span class="line">        [</span><br><span class="line">            <span class="string">&quot;@babel/preset-env&quot;</span></span><br><span class="line">        ]</span><br><span class="line">    ],</span><br><span class="line">    <span class="string">&quot;plugins&quot;</span>: [</span><br><span class="line">        [</span><br><span class="line">            <span class="string">&quot;@babel/plugin-transform-runtime&quot;</span>,</span><br><span class="line">            &#123;</span><br><span class="line">                corejs: &#123;<span class="attr">version</span>: <span class="number">3</span>, <span class="attr">proposals</span>: <span class="literal">true</span>&#125;,</span><br><span class="line">                helpers: <span class="literal">true</span>,</span><br><span class="line">                useESModules: <span class="literal">true</span>,</span><br><span class="line">                regenerator: <span class="literal">true</span>,</span><br><span class="line">                absoluteRuntime: <span class="string">&quot;./node_modules&quot;</span></span><br><span class="line">            &#125;</span><br><span class="line">        ]</span><br><span class="line">    ]</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p><code>@babel/preset-env：</code>es6 转 es5，翻译JS新的api<br><code>@babel/plugin-transform-runtime：</code>es新特性转换，将转换的代码提共通方法。减小代码量，减小打包大小。</p>
<blockquote>
<p><code>注：配置babel前，需要配置browserslist，我们直接在package.json中设置</code>  </p>
</blockquote>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&quot;browserslist&quot;: [</span><br><span class="line">  &quot;&gt; 1%&quot;,</span><br><span class="line">  &quot;last 2 versions&quot;,</span><br><span class="line">  <span class="string">&quot;not ie &lt;= 8&quot;</span></span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h2 id="webpack-config-js文件-webpack配置文件"><a href="#webpack-config-js文件-webpack配置文件" class="headerlink" title="webpack.config.js文件(webpack配置文件)"></a>webpack.config.js文件(webpack配置文件)</h2><p>在项目根目录下创建<code>webpack.config.js</code>文件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>);</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    entry: path.resolve(__dirname, <span class="string">&#x27;src&#x27;</span>),</span><br><span class="line">    output: &#123;</span><br><span class="line">        path: path.resolve(__dirname, <span class="string">&#x27;dist&#x27;</span>),</span><br><span class="line">        filename: <span class="string">&#x27;main.js&#x27;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="built_in">module</span>: &#123;</span><br><span class="line">        rules: [</span><br><span class="line">            &#123;</span><br><span class="line">                test: <span class="regexp">/\.js$/</span>,</span><br><span class="line">                exclude: <span class="regexp">/(node_modules|bower_components)/</span>,</span><br><span class="line">                include: path.resolve(__dirname, <span class="string">&#x27;src&#x27;</span>),</span><br><span class="line">                use: &#123;</span><br><span class="line">                    loader: <span class="string">&#x27;babel-loader&#x27;</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p><code>exclude:</code>排除路径<br><code>include:</code>包含路径</p>
<h2 id="执行yarn-build命令打包，对比生成的前后文件"><a href="#执行yarn-build命令打包，对比生成的前后文件" class="headerlink" title="执行yarn build命令打包，对比生成的前后文件"></a>执行yarn build命令打包，对比生成的前后文件</h2><ul>
<li>使用babel前：<ul>
<li>存在箭头函数、const关键字等es6+语法<br>  <img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7a0e872f73bc4a8f892825d477e8cff1~tplv-k3u1fbpfcp-watermark.image"></li>
</ul>
</li>
<li>使用babel后：<ul>
<li>ES6+语法转换成为ES5语法<br>  <img src="https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6e08bd4415074996b84325d229d36a29~tplv-k3u1fbpfcp-watermark.image"></li>
</ul>
</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%BA%8C/" data-id="ckjsoehcf0001fsr00sgpcwhw" data-title="Webpack基础配置(二)" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%89/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Webpack基础配置(三)
        
      </div>
    </a>
  
  
    <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%80/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Webpack基础配置(一)</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/01/">January 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/12/">December 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/01/11/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E5%9B%9B/">Webpack基础配置(四)</a>
          </li>
        
          <li>
            <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%89/">Webpack基础配置(三)</a>
          </li>
        
          <li>
            <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%BA%8C/">Webpack基础配置(二)</a>
          </li>
        
          <li>
            <a href="/2020/12/20/Webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE-%E4%B8%80/">Webpack基础配置(一)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>